package com.ruoyi.business.controller.fuyou;


import com.ruoyi.business.service.*;
import com.ruoyi.common.annotation.Anonymous;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.dto.*;
import com.ruoyi.common.core.domain.entity.*;
import com.ruoyi.common.core.domain.table.OrderFoodVOTable;
import com.ruoyi.common.core.domain.table.RefundManagementTable;
import com.ruoyi.common.core.domain.vo.OrderPeopleNumVO;
import com.ruoyi.common.core.domain.vo.RefundManagementVO;

import com.ruoyi.common.core.domain.vo.WsdCanteensOrderVO;
import com.ruoyi.common.core.domain.vo.WsdRefundTransactionsVO;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.utils.poi.ExcelUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;



/**
 *
 * 退款流水表，记录所有退款相关的信息
 *
 * @author wwq
 * @since 2024-05-20
 */
@RestController
@RequestMapping("/wsdRefundTransactions")
public class WsdRefundTransactionsController extends BaseController {
    @Autowired
    private IWsdRefundTransactionsService wsdRefundTransactionsService;

    @Autowired
    private IWsdLeavesService wsdLeavesService;
    @Autowired
    private IWsdStudentsService wsdStudentsService;
    @Autowired
    private IWsdMealRefundsService wsdMealRefundsService;
    /**
     * 查询退款流水记录列表
     */
    @GetMapping("/list")
    public TableDataInfo list(WsdPaymentRecordsDTO wsdPaymentRecordsDTO) {
        startPage();
        List<WsdRefundTransactionsVO> list = wsdRefundTransactionsService.listRefund(wsdPaymentRecordsDTO);
        return getDataTable(list);
    }

    /**
     * 查询退款流水表记录列表
     */
//    @GetMapping("/list2")
//    public TableDataInfo list2(WsdRefundTransactions wsdRefundTransactions)
//    {
//        startPage();
//        List<WsdRefundTransactions> list = wsdRefundTransactionsService.selectWsdRefundTransactionsList(wsdRefundTransactions);
//        return getDataTable(list);
//    }

    @GetMapping("/listReturn")
    public TableDataInfo listReturn(WsdLeaves wsdLeaves) {
//        wsdLeaveRecords.setPayoutStatus( 2L );
        wsdLeaves.setStatus( 1L );
        WsdStudents wsdStudents = wsdStudentsService.selectWsdStudentsById(wsdLeaves.getStudentId());
        if (wsdStudents==null){
            List<WsdLeaves> list = new ArrayList<>();
            return getDataTable(list);
        }
        wsdLeaves.setSchoolId( wsdStudents.getSchoolId() );
        startPage();
        List<WsdLeaves> list = wsdLeavesService.wsdLeavesList(wsdLeaves);
        //List<WsdRefundTransactionsVO> list = wsdRefundTransactionsService.listRefund(wsdPaymentRecordsDTO);
        return getDataTable(list);
    }

    /**
     * 小程序，退款详情
     * @param id
     * @return
     */
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") Long id) {
        BigDecimal addAmount = BigDecimal.ZERO;
        Integer refundNum = 0;
        List<RefundsList> refundsList = new ArrayList<>();
        WsdLeaves wsdLeaves = wsdLeavesService.getById(id);
        WsdMealRefundsGetDTO wsdMealRefundsGetDTO = new WsdMealRefundsGetDTO();
        BeanUtils.copyProperties(wsdLeaves, wsdMealRefundsGetDTO);
        WsdMealRefunds wsdMealRefunds = new WsdMealRefunds();
        wsdMealRefunds.setLeaveId(wsdLeaves.getId());
        //wsdMealRefunds.setStatus( 1 );
        List<WsdMealRefunds> list = wsdMealRefundsService.selectWsdMealRefundsList(wsdMealRefunds);
        for (WsdMealRefunds wsdMealRefunds1 : list){
            addAmount = addAmount.add(wsdMealRefunds1.getRefundAmount());
            if (wsdMealRefunds1.getBreakfast()){
                refundNum++;
                RefundsList refundsList1 = new RefundsList();
                refundsList1.setRefundName( "早餐" );
                refundsList1.setRefundDate( wsdMealRefunds1.getRefundDate() );
                refundsList.add(refundsList1);
            }
            if (wsdMealRefunds1.getLunch()){
                refundNum++;
                RefundsList refundsList1 = new RefundsList();
                refundsList1.setRefundName( "午餐" );
                refundsList1.setRefundDate( wsdMealRefunds1.getRefundDate() );
                refundsList.add(refundsList1);
            }
            if (wsdMealRefunds1.getDinner()){
                refundNum++;
                RefundsList refundsList1 = new RefundsList();
                refundsList1.setRefundName( "晚餐" );
                refundsList1.setRefundDate( wsdMealRefunds1.getRefundDate() );
                refundsList.add(refundsList1);
            }
        }
        wsdMealRefundsGetDTO.setRefundNum( refundNum );
        wsdMealRefundsGetDTO.setRefundsList( refundsList );
        wsdMealRefundsGetDTO.setAddAmount( addAmount );
        return success(wsdMealRefundsGetDTO);
    }

    /**
     * 退款管理
     * @param wsdPaymentRecordsDTO
     * @return
     */
    @GetMapping("/refundManagementList")
    public TableDataInfo refundManagementList(WsdRefundManagementDTO wsdPaymentRecordsDTO) {
        startPage();
        List<RefundManagementVO> list = wsdRefundTransactionsService.refundManagementList(wsdPaymentRecordsDTO);
        return getDataTable(list);
    }
    //详情
    @GetMapping("/refundFood/{id}")
    public AjaxResult refundFood(@PathVariable("id") Long id) {
        List<OrderFoodVOTable> orderFoodVOTableList = wsdRefundTransactionsService.refundFood(id);
        return success(orderFoodVOTableList);
    }
    /**
     * 退餐人数
     * @param
     * @return
     */
    @GetMapping("/refundPeopleNum")
    public AjaxResult orderPeopleNum(WsdRefundManagementDTO wsdPaymentRecordsDTO) {
        SysUser user = getLoginUser().getUser();
        if (!user.isAdmin()){
            Long schoolId = user.getSchoolId();
            wsdPaymentRecordsDTO.setSchoolId(schoolId);
        }
        OrderPeopleNumVO orderPeopleNumVO = wsdRefundTransactionsService.orderPeopleNum(wsdPaymentRecordsDTO);
        return success(orderPeopleNumVO);
    }
    @PostMapping("/exportRefundManagement")
    public void exportOrder(HttpServletResponse response, WsdRefundManagementDTO wsdPaymentRecordsDTO) {
        List<RefundManagementVO> list = wsdRefundTransactionsService.refundManagementList(wsdPaymentRecordsDTO);
        List<RefundManagementTable> refundManagementTableList = new ArrayList<>();
        list.forEach(item->{
            RefundManagementTable refundManagementTable = new RefundManagementTable();
            BeanUtils.copyProperties(item, refundManagementTable);
            refundManagementTableList.add(refundManagementTable);
        });
        ExcelUtil<RefundManagementTable> util = new ExcelUtil<RefundManagementTable>(RefundManagementTable.class);
        util.exportExcel(response, refundManagementTableList, "退餐管理");
    }
}
